<?php

class Admin_activites extends Admin_Activites_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Activites_m');
        $this->_data['sidebar'] = "activites";
    }

    function index() {
        is_admin();
        $this->list_actualite();
    }

    function list_actualite() {
        is_admin();
        $this->_data['query'] = $this->Activites_m->list_content('tblactualite');
        $this->_data['page_title'] = "Liste actualités";
        $this->display_admin('admin/activites/list_actualite');
    }

    function add_actualite() {
        is_admin();
        $config = array(
            array(
                'field' => 'title',
                'label' => 'Titre',
                'rules' => 'required'
            ),
            array(
                'field' => 'content',
                'label' => 'Contenu',
                'rules' => 'required'
            ),
            array(
                'field' => 'description',
                'label' => 'Description',
                'rules' => 'required|xss_clean'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {

                $data = array(
                    'title' => $this->input->post('title'),
                    'description' => $this->input->post('description'),
                    'content' => $this->input->post('content')
                );
                $query = $this->Activites_m->add_data('tblactualite', $data);
                if ($query)
                    $this->session->set_flashdata('error', "L'article a été ajouté !");
                else
                    $this->session->set_flashdata('error', "L'article n'est pas ajouté, veuillez reessayer !");
                redirect('admin_activites/list_actualite');
            }
        }


        $this->display_admin('admin/activites/add_actualite_form');
    }

    function update_actualite($id = "") {
        is_admin();

        $config = array(
            array(
                'field' => 'title',
                'label' => 'Titre',
                'rules' => 'required|xss_clean'
            ),
            array(
                'field' => 'content',
                'label' => 'Contenu',
                'rules' => 'required|xss_clean'
            ),
            array(
                'field' => 'description',
                'label' => 'Description',
                'rules' => 'required|xss_clean'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {

                $data = array(
                    'title' => $this->input->post('title'),
                    'content' => $this->input->post('content'),
                    'description' => $this->input->post('description')
                );
                $query = $this->Activites_m->update_data('tblActualite', $data, $id);
                if ($query == 1)
                    $this->session->set_flashdata('error', "L'article a été modifié !");
                else
                    $this->session->set_flashdata('error', "L'article n'est pas modifié, veuillez reessayer !");
                redirect('admin_activites/list_actualite');
            }
        }

        $this->_data['query'] = $this->Activites_m->get_details('tblactualite', $id);
        $this->display_admin('admin/activites/update_actualite_form');
    }

    function delete_actualite($id) {
        is_admin();

        $query = $this->Activites_m->delete_data('tblactualite', $id);
        if ($query == 1)
            $this->session->set_flashdata('error', "L'article a été supprimé !");
        else
            $this->session->set_flashdata('error', "L'article n'a pas été supprimé !");
        redirect('admin_activites/list_actualite');
    }

    function list_reference() {
        is_admin();
        $data = $this->Activites_m->list_content("tblreference");
        $this->_data['query'] = $data;
        $this->display_admin('admin/activites/list_reference_form');
    }

    function update_reference($id) {
        is_admin();

        $config = array(
            array(
                'field' => 'title',
                'label' => 'Titre',
                'rules' => 'required|xss_clean'
            ),
            array(
                'field' => 'content',
                'label' => 'Contenu',
                'rules' => 'required|xss_clean'
            )
          
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $image_path = './assets/images/reference';
                $thumb_path = $image_path . '/thumbs';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 2000,
                    'encrypt_name' => true
                );
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload()) {
                    $data = array(
                        'title' => $this->input->post('title'),
                        'content' => $this->input->post('content')
                        
                    );
                    $query = $this->Activites_m->update_data('tblreference', $data, $id);
                    if ($query == 1)
                        $this->session->set_flashdata('error', "L'article a été modifié !");
                    else
                        $this->session->set_flashdata('error', "L'article n'est pas modifié, veuillez reessayer !");
                    redirect('admin_activites/list_reference');
                } else {
                    $query = $this->Activites_m->get_details("tblreference", $id);
                    if (!empty($query)) {
                        $info = $query;
                        if ($info->image != null) {
                            $image_to_delete = './assets/images/reference/' . $info->image;
                            $thumb_to_delete = './assets/images/reference/thumbs/' . $info->image;
                            delete_images($image_to_delete, $thumb_to_delete);
                        }
                    }
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    $data = array(
                        'title' => $this->input->post('title'),
                        'image' => $upload_info['file_name'],
                        
                        'content' => $this->input->post('content')
                    );
                   $query = $this->Activites_m->update_data('tblreference', $data, $id);
                    if ($query == 1)
                        $this->session->set_flashdata('error', "L'article a été modifié !");
                    else
                        $this->session->set_flashdata('error', "L'article n'est pas modifié, veuillez reessayer !");
                    redirect('admin_activites/list_reference');
                }
            }
        }
       
        $this->_data['query'] = $this->Activites_m->get_details("tblreference", $id);
        $this->display_admin('admin/activites/update_reference_form');
    }

}

?>
